package com.metadata_system.api_server.socket_server;

import com.alibaba.fastjson.JSON;
import com.metadata_system.common.db.entity.ChatMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.utils.DateTimeUtil;

import java.util.*;

@Service
@Slf4j
public class MySocketIOService {

    @Value("${spring.profiles.active}")
    public String springProfilesActive;

    @Autowired
    MyClientUser myClientUser;


    public static Integer maxPushNums = 2;

    // N秒前发送的
    public static Integer diffSeconds = 3;


    public static String setUUID(String clientMessage, String messageUUID) {
        Map clientMessageMap = new HashMap();
        try {
            clientMessageMap = JSON.parseObject(clientMessage, Map.class);
        } catch (Exception e) {
            clientMessageMap.put("message", clientMessage);
        }
        clientMessageMap.put("messageUUID", messageUUID);
        return JSON.toJSONString(clientMessageMap);
    }


    /**
     * 单人接收消息
     *
     * @param chatMessageJSON
     * @return
     */
    public ApiResult sendMessage(String chatMessageJSON) {
        MyMessage myMessageParams = JSON.parseObject(chatMessageJSON, MyMessage.class);
        if (myMessageParams.getUserId() == null) {
            return ApiResult.error("发送消息体有误");
        }
        ApiResult sendResult = myClientUser.sendMessageToUser(MyMessage.builder()
                .userId(myMessageParams.getUserId())
                .message(myMessageParams.getMessage())
                .eventType(myMessageParams.getEventType())
                .build());

        return sendResult;
    }



}
